const app = require("../app");
const {
  NAME_OR_PASSWORD_IS_REQUIERD,
  NAME_IS_ALREADY_EXISTS,
  NAMW_IS_NOT_EXISTS,
  PASSWORD_IS_INCORRENT,
  UNAUTHORIZATION,
  VOIDAUTHORIZATION,
  OPERATION_IS_NOT_ALLOWED,
} = require("../config/error");

app.on("error", (err, ctx) => {
  let code = 0;
  let message = "";
  switch (err) {
    case NAME_OR_PASSWORD_IS_REQUIERD:
      (code = -1001), (message = "姓名或者密码不能为空");
      break;
    case NAME_IS_ALREADY_EXISTS:
      (code = -1002), (message = "用户已经存在！");
      break;
    case NAMW_IS_NOT_EXISTS:
      code = -1003;
      message = "用户不存在！";
      break;
    case PASSWORD_IS_INCORRENT:
      code = -1004;
      message = "密码错误！";
      break;
    case UNAUTHORIZATION:
      code = -1005;
      message = "无效的token";
      break;
    case VOIDAUTHORIZATION:
      code = -1006;
      message = "token已过期";
      break;
    case OPERATION_IS_NOT_ALLOWED:
      code = -2001;
      message = "没有操作该资源的权限";
      break;
  }
  ctx.body = {
    code,
    message
  };
});